<template>
  <div class="box hide-scroller-bar">
    <a-page-header
      title="会员开通"
      @back="() => $router.back()"
      style="padding: 10px 24px"
    />
    <div class="px-20">
      <a-card class="bg-white mx-20">
        <div class="flex align-center">
          <span class="fs-16">选择套餐</span>
          <div class="ml-10" style="width: 30px">
            <img :src="ai16Png" class="w-full h-full" />
          </div>
        </div>
        <div class="mt-10 flex flex-row flex-wrap" style="gap: 20px">
          <a-card
            class="package-card cursor-pointer relative"
            :class="{ active: selected == index }"
            @click="() => (selected = index)"
            v-for="(item, index) in packages"
            :key="index"
          >
            <div style="line-height: 1">
              {{ item.name }}
            </div>
            <div
              class="fs-28 bold-500 mt-10"
              style="line-height: 0.8"
              v-if="item.money > 0"
            >
              <span>￥{{ item.money }}</span>
              <span class="origin-price" v-if="item.discount_price > 0"
                >￥{{ item.original_price }}</span
              >
            </div>
            <div
              class="fs-18 bold-500 mt-15 text-red"
              style="line-height: 0.8"
              v-else
            >
              请联系客服
            </div>
            <div class="absolute r-0 t-0 vip-team-tag" v-if="item.is_team">
              团队
            </div>
            <div
              class="absolute r-0 t-0 discount-tag"
              v-else-if="item.discount_price > 0"
            >
              立省￥{{ item.discount_price }}
            </div>
          </a-card>
        </div>
        <div class="mt-40">
          <span class="fs-16">包含权益</span>
          <span v-if="spread_explain" class="spread-explain">{{
            spread_explain
          }}</span>
          <a-button
            type="primary"
            shape="round"
            ghost
            @click="genShareLink"
            size="small"
            style="font-size: 12px; margin-left: 10px"
          >
            <CopyOutlined />
            复制邀请链接
          </a-button>
        </div>
        <div class="mt-20">
          <a-card class="fs-12 include-card">
            {{ selectedPackage.info }}
          </a-card>
        </div>
        <div class="mt-40" v-if="selectedPackage.money > 0">
          <span class="fs-16">开通费用</span>
        </div>
        <div class="fs-28 text-red font-bold">
          <template v-if="selectedPackage.money > 0">
            ￥{{ selectedPackage.money }}
          </template>
        </div>
        <div class="mt-20 flex align-center">
          <a-button
            type="primary"
            @click="openPackageHandle"
            v-if="selectedPackage.money > 0"
            :loading="loading"
            >立即开通</a-button
          >
          <a-button
            type="primary"
            @click="() => (modalVisible = 'contact')"
            class="contactBtn"
            v-else
            >联系客服</a-button
          >
          <div class="ml-30 flex align-center">
            <div style="width: 20px; height: 20px">
              <img
                :src="ai15Png"
                class="w-full h-full"
                style="vertical-align: top"
              />
            </div>
            <div class="ml-5 cursor-pointer">
              <a-button
                type="link"
                @click="vipVisible=true"
                target="_blank"
                class="text-gray-9 agreement-link"
                >我已阅读并同意<span class="text-orange"
                  >《会员服务协议》</span
                ></a-button
              >
            </div>
          </div>
        </div>
        <div class="mt-20 pb-30 fs-12 text-gray-5">
          会员服务为虚拟商品，支付成功后不支持退款
        </div>
      </a-card>
    </div>
    <a-modal
      :visible="modalVisible != ''"
      :title="modalTitle[modalVisible] || ''"
      @cancel="modalVisible = ''"
      :footer="false"
    >
      <template v-if="modalVisible == 'contact'">
        <div class="flex justify-center">
          <div style="width: 200px; height: 200px">
            <img :src="contact_url" class="w-full h-full" v-if="contact_url" />
          </div>
        </div>
      </template>

      <template v-if="modalVisible == 'pay'">
        <div class="flex flex-col text-center">
          <div style="width: 200px; height: 200px; margin: 0 auto">
            <img
              :src="payQrcodeUrl"
              class="w-full h-full"
              v-if="payQrcodeUrl"
            />
          </div>
          <div class="mt-10 fs-12">支付中... 请不要关闭支付窗口</div>
        </div>
      </template>
    </a-modal>
    <ProtocolModal :visible="vipVisible" titleTxt="会员服务协议" :content="pay_agreement_url" @onClose="closeVipModal"></ProtocolModal>
  </div>
</template>

<script>
import {
  defineComponent,
  reactive,
  toRefs,
  computed,
  watch,
  toRaw,
  ref,
} from "vue";
import apiRequest from "../../common/utils/request";
import ai15Png from "../../common/image/ai15.png";
import ai16Png from "../../common/image/ai16.png";
import store from "@/store";
import utils from "../../common/utils/utils";
import { useRequest } from "@/hooks/loading";
import { useRouter } from "vue-router";
import { message } from "ant-design-vue";
import { CopyOutlined } from "@ant-design/icons-vue";
import commonRequest from "../../common/utils/request";
import ProtocolModal from '@/components/ProtocolModal/Index.vue'
export default defineComponent({
  name: "ActivateMembership",
  components: { CopyOutlined,ProtocolModal },
  setup() {
    const router = useRouter();

    const state = reactive({
      vipVisible:false,
      selected: 0,
    });

    const { loading, request } = useRequest();

    const packages = ref([]);

    const getPackageList = () => {
      apiRequest.get("/work_ai", "/order/package/list", {}).then(({ list }) => {
        packages.value = list;
      });
    };

    const selectedPackage = computed(() => {
      return packages.value.length > 0 ? packages.value[state.selected] : {};
    });

    getPackageList();

    const pay_agreement_url = ref("");
    const contact_url = ref("");
    const spread_explain = ref("");

    apiRequest.get("/work_ai", "/config").then(async (res) => {
      if (res.list && res.list.pay_agreement_url) {
        pay_agreement_url.value = res.list.pay_agreement_url;
      }
      if (res.list && res.list.service_qrcode) {
        contact_url.value = res.list.service_qrcode;
      }
      spread_explain.value = res.list.spread_explain;
    });

    let payQrcodeUrl = ref("");
    let checkPayTimer = ref(null);

    const checkPayHandle = (order_id) => {
      checkPayTimer.value = setInterval(() => {
        apiRequest
          .get("/work_ai", "/order/getOrderStatus", {
            order_id: order_id,
          })
          .then((checkResult) => {
            if (checkResult.status > 0) {
              modalVisible.value = "";
              message.success("支付成功，将跳转订单列表页");
              router.push("/order");
            }
          });
      }, 2000);
    };

    const openPackageHandle = () => {
      store.dispatch("GetVillageInfo").then((villageInfo) => {
        let params = {
          team_id: utils.getCurrentTeam(),
          package_id: selectedPackage.value.id,
          from: "ai",
        };

        if (villageInfo) {
          params.village_id = villageInfo.id;
        }

        request(apiRequest.post("/work_ai", "/order/tmpPayOrder", params)).then(
          (res) => {
            let qrcodeParams = {
              qrCode:
                selectedPackage.value.payUrl + "&tmp_order_id=" + res.order_id,
            };

            request(apiRequest.get("/public", "/qrcode", qrcodeParams)).then(
              (result) => {
                payQrcodeUrl.value = result.url;
                modalVisible.value = "pay";

                checkPayHandle(res.order_id);
              }
            );
          }
        );
      });
    };

    const modalVisible = ref("");
    const modalTitle = {
      contact: "联系客服",
      pay: "扫码支付",
    };

    watch(modalVisible, (newVisible, oldVisible) => {
      if (newVisible == "" && oldVisible == "pay") {
        clearInterval(checkPayTimer.value);
      }
    });

    const genShareLink = () => {
      commonRequest.post("/work_ai", "/user/code").then((res) => {
        if (res.result && res.result.url) {
          let link = `注册即送免费试用机会，戳链接开始使用：${res.result.url}`;
          utils.copyText(link);
        }
      });
    };
    const closeVipModal = ()=>{
      state.vipVisible = false
    }
    return {
      ...toRefs(state),
      ai16Png,
      ai15Png,
      pay_agreement_url,
      packages,
      selectedPackage,
      openPackageHandle,
      modalVisible,
      modalTitle,
      contact_url,
      loading,
      payQrcodeUrl,
      spread_explain,
      genShareLink,
      closeVipModal,
    };
  },
});
</script>

<style lang="less" scoped>
.box {
  width: 100%;
  height: 100%;
  overflow: hidden auto;
}

.package-card {
  position: relative;
  width: 200px;
  box-sizing: border-box;
  border-radius: 10px;

  &.active {
    border: 1px solid #f1c581;
    background-color: #fdf9f0;
  }

  :deep(.ant-card-body) {
    padding: 16px;
  }
}

.include-card {
  background-color: #f9f0e8;
  width: 50%;
  border-radius: 4px;

  :deep(.ant-card-body) {
    padding: 12px;
  }
}

.spread-explain {
  color: @primary-color;
  margin-left: 40px;
}

.origin-price {
  margin-left: 10px;
  font-size: 18px;
  text-decoration: line-through;
  color: #999;
  font-weight: 300;
}

.discount-tag {
  background-color: red;
  color: #fff;
  font-size: 12px;
  padding: 1px 8px;
  border-radius: 0 10px;
}

.vip-team-tag {
  background-color: #4c83fc;
  color: #fff;
  font-size: 12px;
  padding: 1px 8px;
  border-radius: 0 10px;
}

.agreement-link:hover {
  color: #262626;
}

.contactBtn {
  background-color: #52c41a;
  border: 1px solid #52c41a;

  &:hover {
    background-color: #57ce1b;
    border: 1px solid #57ce1b;
  }
}
</style>
